define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/example/mycat/mycatdemo.js'
], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var _self = this;
        return {
          urls: {
            index: backend.mycatService + '/api/mycatdemo/',
            indextest: backend.mycatService + '/api/mycatdemo/test',
            indexyusp: backend.mycatService + '/api/mycatdemo/yusp',
            show: backend.mycatService + '/api/mycatdemo/',
            create: backend.mycatService + '/api/mycatdemo/',
            update: backend.mycatService + '/api/mycatdemo/update/',
            batchdelete: backend.mycatService + '/api/mycatdemo/batchdelete/'
          },
          tableColumns: [
            { label: this.$t('mycatdemo.zj'), prop: 'id', resizable: true },
            { label: this.$t('mycatdemo.xm'), prop: 'name', resizable: true },
            { label: this.$t('mycatdemo.dz'), prop: 'address', resizable: true }
          ],
          updateFields: [
            {
              columnCount: 2,
              fields: [
                {label: this.$t('mycatdemo.zj'), field: 'id', type: 'input', rules: [{ required: true, message: this.$t('mycatdemo.btx')}]},
                {label: this.$t('mycatdemo.xm'), field: 'name', type: 'input', rules: [{ required: false, message: this.$t('mycatdemo.btx')}]},
                {label: this.$t('mycatdemo.dz'), field: 'address', type: 'input', rules: [{ required: false, message: this.$t('mycatdemo.btx')}]}
              ]
            }
          ],
          queryFields: [
            { placeholder: this.$t('mycatdemo.zj'), field: 'id', type: 'input' },
            { placeholder: this.$t('mycatdemo.xm'), field: 'name', type: 'input' },
            { placeholder: this.$t('mycatdemo.dz'), field: 'address', type: 'input' }
          ],
          baseParams: {
            condition: {
            }
          },
          queryButtons: [
            {
              label: this.$t('mycatdemo.ss'),
              op: 'submit',
              type: 'primary',
              icon: 'search',
              click: function (model, valid) {
                if (valid) {
                  var param = { condition: JSON.stringify(model) };
                  _self.$refs.reftable.remoteData(param);
                }
              }
            },
            { label: this.$t('mycatdemo.zz'), op: 'reset', type: 'primary', icon: 'yx-loop2' }
          ],
          updateButtons: [
            {
              label: this.$t('mycatdemo.qx'),
              type: 'primary',
              icon: 'yx-undo2',
              hidden: false,
              click: function (model) {
                _self.dialogVisible = false;
              }
            },
            {
              label: this.$t('mycatdemo.bc'),
              type: 'primary',
              icon: 'check',
              hidden: false,
              click: function (model) {
                var validate = false;
                _self.$refs.reform.validate(function (valid) {
                  validate = valid;
                });
                if (!validate) {
                  return;
                }
                var formUrl = _self.urls.create;
                if (_self.viewType == 'EDIT') {
                  formUrl = _self.urls.update;
                }
                yufp.service.request({
                  method: 'POST',
                  url: formUrl,
                  data: model,
                  callback: function (code, message, response) {
                    if (code == 0) {
                      _self.$refs.reftableyusp.remoteData();
                      _self.$refs.reftabletest.remoteData();
                      _self.$refs.reftable.remoteData();

                      _self.$message(_self.$t('mycatdemo.czcg'));
                      _self.dialogVisible = false;
                    }
                  }
                });
              }
            }
          ],
          height: yufp.frame.size().height - 103,
          dialogVisible: false,
          formDisabled: false,
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false)
        };
      },
      methods: {
        /**
                * @param viewType 表单类型
                * @param editable 可编辑,默认false
                */
        switchStatus: function (viewType, editable) {
          var _self = this;
          _self.viewType = viewType;
          _self.updateButtons[1].hidden = !editable;
          _self.formDisabled = !editable;
          _self.dialogVisible = true;
        },
        addFn: function () {
          var _self = this;
          _self.switchStatus('ADD', true);
          _self.$nextTick(function () {
            _self.$refs.reform.resetFields();
          });
        },
        modifyFn: function () {
          if (this.$refs.reftable.selections.length != 1) {
            this.$message({ message: this.$t('mycatdemo.qxxzytjl'), type: 'warning' });
            return;
          }
          this.switchStatus('EDIT', true);
          this.$nextTick(function () {
            var obj = this.$refs.reftable.selections[0];
            yufp.extend(this.$refs.reform.formModel, obj);
          });
        },
        infoFn: function () {
          if (this.$refs.reftable.selections.length != 1) {
            this.$message({ message: this.$t('mycatdemo.qxxzytjl'), type: 'warning' });
            return;
          }
          this.switchStatus('DETAIL', false);
          this.$nextTick(function () {
            yufp.extend(this.$refs.reform.formModel, this.$refs.reftable.selections[0]);
          });
        },
        deleteFn: function () {
          var _self = this;
          var selections = _self.$refs.reftable.selections;
          if (selections.length < 1) {
            _self.$message({ message: _self.$t('mycatdemo.qxxzytjl'), type: 'warning' });
            return;
          }
          var len = selections.length, arr = [];
          for (var i = 0; i < len; i++) {
            arr.push(selections[i].id);
          }
          yufp.service.request({
            method: 'POST',
            url: _self.urls.batchdelete + arr.join(','),
            callback: function (code, message, response) {
              if (code == 0) {
                _self.$refs.reftable.remoteData();
                _self.$message(_self.$t('mycatdemo.czcg'));
              }
            }
          });
        }
      }
    });
  };
});
